<?php
namespace Api\Controller;
use Think\Controller;
class UserCenterController extends CommonController{

    //实例化用户
    protected $currentUser;
    
    function _initialize() {
        parent::_initialize();
        
        //判断token是否存在
        $token = I('post.token');

        $userMap['token'] = $token;
        $user = $this->userModel->where($userMap)->find();

        if(isNullOrEmpty($user)) {
            $this->errorReturn('ERROR_TOKEN');
        }

        $this->currentUser = $user;
        
    }

    //用户确认完成订单
    public function endOrder() {

        $orderId = I("post.orderId");

        $order = $this->orderModel->queryOrderInfo($orderId, $this->currentUser["user_id"]);

        if($order["status"] != C("ORDER_STATUS.WAITING_SERVICE")) {
            $this->errorReturn('ERROR_ORDER_NOT_FOUND');
        }

        $master = $this->userModel->queryById($order["master_id"]);

        $level = $this->levelModel->queryById($master["level_id"]);

        $orderData["status"] = C("ORDER_STATUS.END_SERVICE");

        $this->orderModel->where(array("order_id"=>$order["order_id"]))->save($orderData);

        //给打手放款 流水
        $accountDetailData["detail_type"] = C("ACCOUNT_DETAIL_TYPE.INCOME");
        $accountDetailData["money"] = $order["total_account"]-$level["service_charge"];
        $accountDetailData["create_time"] = getCurrentDate();
        $accountDetailData["order_id"] = $order["order_id"];
        $accountDetailData["user_id"] = $order["master_id"];

        $this->accountDetailModel->add($accountDetailData);

        //增加总额
        $master["total_assets"] = $master["total_assets"] + $order["total_account"] - $level["service_charge"];
        $master["order_number"] = $master["order_number"] + 1;

        $this->userModel->updateObj($master);

        $this->response($this->returnData, 'json');

    }

    //申请退款
    public function refundOrder() {

        $orderId = I("post.orderId");

        $order = $this->orderModel->queryOrderInfo($orderId, $this->currentUser["user_id"]);

        if($order["status"] == C("ORDER_STATUS.WAITING_SERVICE")) {
            $this->errorReturn('ERROR_ORDER_NOT_FOUND');
        }

        $orderData["status"] = C("ORDER_STATUS.WAITING_REFUND");

        $this->orderModel->where(array("order_id"=>$order["order_id"]))->save($orderData);

        //添加申请退款
        $drawbackData["user_id"] = $this->currentUser["user_id"];
        $drawbackData["money"] = $order["total_amount"];
        $drawbackData["order_id"] = $order["order_id"];
        $drawbackData["reason"] = I("post.reason");
        $drawbackData["status"] = C("DRAWBACK_STATUS.WAITING");
        $drawbackData["create_time"] = getCurrentDate();

        $this->drawbackModel->add($drawbackData);

        $this->response($this->returnData, 'json');

    }

    //取消订单
    public function cancelOrder() {

        $orderId = I("post.orderId");

        $order = $this->orderModel->queryOrderInfo($orderId, $this->currentUser["user_id"]);

        if($order["status"] == C("ORDER_STATUS.WAITING_CONFIRM")) {
            $this->errorReturn('ERROR_ORDER_NOT_FOUND');
        }

        $orderData["status"] = C("ORDER_STATUS.USER_CANCEL");

        $this->orderModel->where(array("order_id"=>$order["order_id"]))->save($orderData);

        $this->response($this->returnData, 'json');

    }

    //申请成为喝酒师傅
    public function applyMaster() {

        $result = $this->masterApplyModel->createApply($this->currentUser, I("post.regionIds"));

        $this->errorReturn($result);

    }

    //查看申请详情
    public function viewMyApply() {

        $info = $this->masterApplyModel->queryByUserId($this->currentUser["user_id"]);

        $info["sex"] = $this->currentUser["sex"];

        $this->returnData['data']['info'] = $info;

        $this->response($this->returnData, 'json');

    }

    //获取我的订单列表
    public function myOrderList() {

        $pageNum = I('post.pageNum') ? I('post.pageNum') : C('DEFAULT_PAGE');

        $condition["status"] = I("post.status");
        $condition["user_id"] = $this->currentUser["user_id"];

        $list = $this->orderModel->queryListByUserAndStatus($condition, $pageNum);

        $this->returnData['data']['list'] = $list;

        $this->response($this->returnData, 'json');

    }

    //获取我的订单详情
    public function myOrderInfo() {

        $orderId = I("post.orderId");

        $order = $this->orderModel->queryOrderInfo($orderId, $this->currentUser["user_id"]);

        if(isNullOrEmpty($order)) {
            $this->errorReturn('ERROR_ORDER_NOT_FOUND');
        }

        $order["service_location_list"]
            = $this->regionRelationshipModel->getMasterServiceList($order["master_id"]);

        $this->returnData['data']['info'] = $order;

        $this->response($this->returnData, 'json');

    }

    //个人设置页
    public function mySettingIndex() {

        $info["headimgurl"] = $this->currentUser["headimgurl"];
        $info["nickname"] = $this->currentUser["nickname"];
        $info["sex"] = $this->currentUser["sex"];
        $info["sign"] = $this->currentUser["sign"];

        $this->returnData['data']['info'] = $info;

        $this->response($this->returnData, 'json');

    }

    //保存我的设置
    public function saveMySetting() {

        $this->currentUser["headimgurl"] = I("post.headimgurl");
        $this->currentUser["nickname"] = I("post.nickname");
        $this->currentUser["sex"] = I("post.sex");
        $this->currentUser["sign"] = I("post.sign");

        $this->userModel->updateObj($this->currentUser);

        $this->response($this->returnData, 'json');

    }

    //修改密码
    public function modifyPassword() {

        $oldPassword = I("post.oldPassword");
        $newPassword = I("post.newPassword");

        if(md5($oldPassword) != $this->currentUser["password"]) {
            $this->errorReturn('ERROR_OLD_PASSWORD');
        }

        $this->currentUser["password"] = md5($newPassword);

        $this->userModel->updateObj($this->currentUser);

        $this->response($this->returnData, 'json');

    }

    public function myCollectList() {

        $pageNum = I('post.pageNum') ? I('post.pageNum') : C('DEFAULT_PAGE');

        $list = $this->collectModel->queryListByUserId($this->currentUser["user_id"], $pageNum);

        $this->returnData['data']['list'] = $list;

        $this->response($this->returnData, 'json');

    }

}